package org.domus.sipnmove;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class QuickSocket {

	public static void openSocket(String ip, Integer port) {
		try {

			Socket socket;
			socket = new Socket(ip, port);

			System.out.println("SOCKET = " + socket);

			BufferedReader plec = new BufferedReader(new InputStreamReader(socket.getInputStream()));

			PrintWriter pred = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

			String str = "bonjour";
			for (int i = 0; i < 10; i++) {
				pred.println(str); // envoi d'un message
				str = plec.readLine(); // lecture de l'écho
			}
			System.out.println("END"); // message de terminaison
			pred.println("END");
			plec.close();
			pred.close();
			socket.close();

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
